package cn.work.dao;
import cn.work.util.DataBaseutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchInReservationDao {
    public List<Map<String, Object>> getAllServices() throws SQLException {
        List<Map<String, Object>> services = new ArrayList<>();
        String sql = "SELECT id, name FROM service";  // 假设表名是 service

        // 这里获取连接
        try (Connection connection = DataBaseutil.getConnection();
             PreparedStatement stmt = connection.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {

            // 迭代查询结果
            while (rs.next()) {
                Map<String, Object> service = new HashMap<>();
                service.put("id", rs.getInt("id"));
                service.put("name", rs.getString("name"));
                services.add(service);
            }
        }
        return services;
    }

    public List<Map<String, Object>> getStylistsByServiceId(int serviceId) throws SQLException {
        List<Map<String, Object>> stylists = new ArrayList<>();
        String sql = """
            SELECT b.id, b.name
            FROM beau b
            JOIN beau_service be
            ON be.beau_id = b.id
            WHERE be.service_id = ?
        """;

        try (Connection connection = DataBaseutil.getConnection();
             PreparedStatement stmt = connection.prepareStatement(sql)) {
            stmt.setInt(1, serviceId);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Map<String, Object> stylist = new HashMap<>();
                    stylist.put("id", rs.getInt("id"));
                    stylist.put("name", rs.getString("name"));
                    stylists.add(stylist);
                }
            }
        }
        for (Map<String, Object> service : stylists) {
            for (Map.Entry<String, Object> entry : service.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
        }
        return stylists;
    }

    
}
